package test.teachMangeSys;

import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 需求分析：
 * 1.添加老师: 通过键盘录入老师信息(id，name，age，birthday)，
 * 使用数组存储老师信息,要求老师的id不能重复，数组中可以可以存储5名老师的信息(5分)
 * 2.删除老师: 通过键盘录入要删除老师的id值,将该老师从数组中移除,
 * 如果录入的id在数组中不存在,需要重新输入老师的id(5分)
 * 3.修改老师: 通过键盘录入要修改老师的id值和修改后的老师信息，
 * 将数组中该老师的信息修改,如果录入的id在数组中不存在,需要重新录入老师的id(5分)
 * 4.查询老师: 将数组中存储的所有老师的信息输出到控制台(5分)
 * 5.退出：程序结束
 *
 *
 * */

// 定义老师类
class Teacher {
    int id;
    String name;
    int age;
    String birthday;

    public Teacher(int id, String name, int age, String birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return "ID: " + id + ", Name: " + name + ", Age: " + age + ", Birthday: "
                + sdf.format(new Date(Long.parseLong(birthday)));
    }
}

public class TeacherManager {
    //数组中存储5名老师信息
    private static final int MAX_TEACHERS = 5;
    private Teacher[] teachers = new Teacher[MAX_TEACHERS];
    private int teacherCount = 0;
    private Scanner scanner = new Scanner(System.in);

    //添加老师
    public void addTeacher() {
        System.out.println("输入老师id");
        int id = scanner.nextInt();
        scanner.nextLine();
        //for(a b:c){},把a类型的集合c中的每个元素赋值给b
        for (Teacher teacher : teachers) {
            if (teacher != null && teacher.id == id) {
                System.out.println("该教师id已经存在，请重试！");
                return;
            }
        }

        System.out.println("请输入教师姓名:");
        String name = scanner.nextLine();
        System.out.println("请输入教师年龄:");
        int age = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Enter teacher birthday (yyyyMMdd):");
        String birthday = scanner.nextLine();

        if (teacherCount < MAX_TEACHERS) {
            teachers[teacherCount++] = new Teacher(id, name, age, birthday);
            System.out.println("教师添加成功！");
        } else {
            System.out.println("无法添加！集合内元素大于"+MAX_TEACHERS);
        }
    }

    public void deleteTeacher() {
        System.out.println("输入需要删除的教师id");
        int id = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i] != null && teachers[i].id == id) {
                teachers[i] = null;
                System.out.println("教师id删除成功！");
                return;
            }
        }

        System.out.println("未找到该ID，请重试！");
    }

    public void updateTeacher() {
        System.out.println("请输入需要更新信息的老师id");
        int id = scanner.nextInt();
        scanner.nextLine(); // consume newline

        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i] != null && teachers[i].id == id) {
                System.out.println("输入新的name");
                String name = scanner.nextLine();

                System.out.println("输入新的age");
                int age = scanner.nextInt();

                System.out.println("输入新的birthday");
                String birthday = scanner.nextLine();

                teachers[i] = new Teacher(id, name, age, birthday);
                System.out.println("信息更新成功");
                return;
            }
        }

        System.out.println("未找到该id，请重试");
    }

    public void queryTeachers() {
        for (Teacher teacher : teachers) {
            if (teacher != null) {
                System.out.println(teacher);
            }
        }
    }

    public void start() {
        while (true) {
            System.out.println("====欢迎进入老师管理系统====");
            System.out.println("\n"+"1. 添加教师");
            System.out.println("2. 删除教师");
            System.out.println("3. 修改老师");
            System.out.println("4. 查询老师");
            System.out.println("5. 退出");
            System.out.print("请输入1~5完成对应操作！");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    deleteTeacher();
                    break;
                case 3:
                    updateTeacher();
                    break;
                case 4:
                    queryTeachers();
                    break;
                case 5:
                    System.out.println("退出系统。");
                    return;
                default:
                    System.out.println("请输入1~5完成对应操作！");
            }
        }
    }

    public static void main(String[] args) {
        TeacherManager manager = new TeacherManager();
        manager.start();
    }
}